package com.im.controller;

import com.im.pojo.Dept;
import com.im.pojo.Staff;
import com.im.pojo.User;
import com.im.service.StaffService;
import com.im.util.JsonResult;
import com.im.util.Key;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class StaffCtrl {
    @Autowired
    private StaffService staffService;
    @RequestMapping(value="/staff",method = RequestMethod.POST)
    public JsonResult add(@RequestBody Staff staff, HttpSession session){
        JsonResult jr=new JsonResult(0,"添加成功！");
        User user=(User)session.getAttribute(Key.SESSION_USER);

        int num=0;
        try {
            num=staffService.save(staff);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        if(num>0){
            jr.setCode(0);
            jr.setMsg("添加成功");
        }else{
            jr.setCode(1);
            jr.setMsg("添加失败");
        }
        return jr;
    }
    @RequestMapping(value="/staff" ,method = RequestMethod.DELETE)
    public JsonResult delete(@RequestBody Staff staff, HttpSession session){
        JsonResult jr=new JsonResult(0,"删除成功！");
        User user=(User)session.getAttribute(Key.SESSION_USER);
        int num=0;
        try {
            num=staffService.delete(staff);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        if(num>0){
            jr.setCode(0);
            jr.setMsg("删除成功");
        }else{
            jr.setCode(1);
            jr.setMsg("删除失败");
        }
        return jr;
    }
    @RequestMapping(value="/staff" ,method = RequestMethod.PUT)
    public JsonResult updateById(@RequestBody Staff staff, HttpSession session){
        JsonResult jr=new JsonResult(0,"成功！");
        User user=(User)session.getAttribute(Key.SESSION_USER);
        int num=0;
        try {
            num=staffService.update(staff);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        if(num>0){
            jr.setCode(0);
            jr.setMsg("修改成功");
        }else{
            jr.setCode(1);
            jr.setMsg("修改失败");
        }
        return jr;
    }
    @RequestMapping(value="/staff" ,method = RequestMethod.GET)
    public JsonResult queryById(@RequestBody Staff staff, HttpSession session){
        JsonResult jr=new JsonResult(0,"成功！");
        User user=(User)session.getAttribute(Key.SESSION_USER);
        try {
            staff=staffService.query(staff);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        if(staff!=null){
            jr.setData(staff);
        }else{
            jr.setCode(1);
            jr.setMsg("查无此数据");
        }
        return jr;
    }
    @RequestMapping(value="/staffshowdept" ,method = RequestMethod.GET)
    public JsonResult querydept(HttpSession session){
        JsonResult jr=new JsonResult(0,"成功！");
        User user=(User)session.getAttribute(Key.SESSION_USER);
        List list=null;
        try {
            list=staffService.querydept();
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        if(list!=null){
            jr.setData(list);
        }else{
            jr.setCode(1);
            jr.setMsg("查无此数据");
        }
        return jr;
    }

    @RequestMapping(value="/staffshowjob" ,method = RequestMethod.GET)
    public JsonResult queryjob(HttpSession session){
        JsonResult jr=new JsonResult(0,"成功！");
        User user=(User)session.getAttribute(Key.SESSION_USER);
        List list=null;
        try {
            list=staffService.queryjob();
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        if(list!=null){
            jr.setData(list);
        }else{
            jr.setCode(1);
            jr.setMsg("查无此数据");
        }
        return jr;
    }


    @RequestMapping(value="/staffs" ,method = RequestMethod.POST)
    public JsonResult queryLikeAll(@RequestBody Staff staff, HttpSession session){
        JsonResult jr=new JsonResult(0,"成功！");
        User user=(User)session.getAttribute(Key.SESSION_USER);
        List list =null;
        try {
            list=staffService.queryAll(staff);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        if(list!=null){
            jr.setData(list);
        }else{
            jr.setCode(1);
            jr.setMsg("查无此数据");
        }
        return jr;
    }
}
